package com.itheima.test.projectSeal;


import com.itheima.enums.NoDrawingsApproveEnum;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.*;

//项目承接前用章申请
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProjectSeal {

    public Map getVaribales() {

        Map<String, Object> variables = new HashMap<>();
        //技术文件用印洽商变更类
      //  variables.put(NoDrawingsApproveEnum.USESEALTYPE.getValue(),NoDrawingsApproveEnum.TECHNOLOGYREGARDINGTHECHANGE.getValue());
        //技术文件用印质量验收类  y有问题
       //variables.put(NoDrawingsApproveEnum.USESEALTYPE.getValue(),NoDrawingsApproveEnum.TECHNOLOGYQUALITYACCEPTANCE.getValue());
         // 技术文件用章 往来联系类
      variables.put(NoDrawingsApproveEnum.USESEALTYPE.getValue(),NoDrawingsApproveEnum.TECHNOLOGYCORRESPONDENCECLASS.getValue());


       //设计主持人
       variables.put(NoDrawingsApproveEnum.DESIGNHOST.getValue(),Arrays.asList("designHost1","designHost2"));

       //审定人
        variables.put(NoDrawingsApproveEnum.VALIDATOR.getValue(),Arrays.asList("validator1","validator2"));
          //部门负责人
        variables.put(NoDrawingsApproveEnum.DEPARTMENTHEAD.getValue(),Arrays.asList("departmentHead1","departmentHead2"));
        //经营负责人
        variables.put(NoDrawingsApproveEnum.MANAGINGDIRECTOR.getValue(),Arrays.asList("managingDirector1","managingDirector2"));
        //项目经理
       variables.put(NoDrawingsApproveEnum.PROJECTMANAGER.getValue(),Arrays.asList("projectManager1","projectManager2"));


     //   variables.put(NoDrawingsApproveEnum.PROJECTMANAGER.getValue(),new ArrayList<>());
        //项目运营部门

        variables.put(NoDrawingsApproveEnum.PROJECTOPERATIONMANAGEMENTDEPARTMENT.getValue(),Arrays.asList("projectOperationManagementDepartment1","projectOperationManagementDepartment2"));

      //专业负责人
        variables.put(NoDrawingsApproveEnum.PERSONINCHARGE.getValue(),Arrays.asList("personInCharge1","personInCharge2"));
          //项目运营管理部门负责人呢

        variables.put(NoDrawingsApproveEnum.PROJECTOPERATIONMANAGEMENTDEPARTMENTDEPARTMENTHEAD.getValue(),Arrays.asList("projectOperationManagementDepartmentDepartmenthead1","projectOperationManagementDepartmentDepartmenthead2"));

        //技术质量部
        variables.put(NoDrawingsApproveEnum.TECHNICALQUALITYDEPARTMENT.getValue(),Arrays.asList("technicalQualityDepartment1","technicalQualityDepartment2"));

        //技术质量部负责人
        variables.put(NoDrawingsApproveEnum.TECHNICALQUALITYDEPARTMENTHEAD.getValue(),Arrays.asList("technicalQualityDepartmentHead1","technicalQualityDepartmentHead2"));


        variables.put(NoDrawingsApproveEnum.PURCHASINGDEPARTMENT.getValue(),Arrays.asList("purchasingDepartment1","purchasingDepartment2"));
        variables.put(NoDrawingsApproveEnum.PURCHASINGDEPARTMENTHEAD.getValue(),Arrays.asList("purchasingDepartmentHead1","purchasingDepartmentHead2"));

      //盖章室
        variables.put(NoDrawingsApproveEnum.SEALCHAMBER.getValue(),Arrays.asList("sealChamber","sealChamber2"));
        // variables.put(NoDrawingsApproveEnum.TECHNICALQUALITYDEPARTMENTHEAD.getValue(),Arrays.asList("technicalQualityDepartmentHead1","technicalQualityDepartmentHead2"));
        return variables;
    }

    /**
     * 流程部署
     */
    @Test
    public void testDeployment() {
//        1、创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        2、获取RepositoryServcie
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        3、使用service进行流程的部署，定义一个流程的名字，把bpmn和png部署到数据中
        Deployment deploy = repositoryService.createDeployment()
                .name("项目用章申请")
                .key("第一次部署")
                //  技术文件用印洽商变更类
               // .addClasspathResource("projectSeal/0908/technologyRegardingTheChange.bpmn20.xml")
                //技术类文件 质量验收类
              // .addClasspathResource("projectSeal/0908/technologyQualityAcceptance.bpmn20.xml")
                // 技术文件用印往来联系类
          //     .addClasspathResource("projectSeal/0908/technologyCorrespondenceClass.bpmn20.xml")
                //奖项申报类
        //      .addClasspathResource("projectSeal/0908/technologyAwardApplicationCategory.bpmn20.xml")
               //技术文件其他技术文件
         //      .addClasspathResource("projectSeal/0908/technologyOtherTechnology.bpmn20.xml")
                // 经营及日常文件用印邀标文件查询函
              //   .addClasspathResource("projectSeal/0908/businessInvitationToTenderDocumentsInquiryLetter.bpmn20.xml")

                // 经营及日常文件用印其它经营日常文件
                .addClasspathResource("projectSeal/0908/businessAndRoutineDocumentsShallBePrintedOnOtherRoutineBusinessDocuments.bpmn20.xml")
                .deploy();

        System.out.println("流程部署id=" + deploy.getId());
        System.out.println("流程部署名字=" + deploy.getName());
        System.out.println("流程部署key=" + deploy.getKey());

    }


    /**
     * 启动流程实例
     * `act_hi_actinst`    流程实例执行历史信息
     * `act_hi_identitylink` 流程参与用户的历史信息
     * `act_hi_procinst`     流程实例的历史信息
     * `act_hi_taskinst`     流程任务的历史信息
     * `act_ru_execution`    流程执行信息
     * `act_ru_identitylink`  流程的正在参与用户信息
     * `act_ru_task`         流程当前任务信息
     */
    @Test
    public void testStartProcess() {
//        1、创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        2、获取RunTimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map varibales = this.getVaribales();
        // 技术文件用印洽商变更类
      // ProcessInstance instance = runtimeService.startProcessInstanceByKey("technologyRegardingTheChange", varibales);
        //技术文件用印质量验收类
    //   ProcessInstance instance =   runtimeService.startProcessInstanceByKey("technologyQualityAcceptance",varibales);
        //技术文件用印往来联系类
       // ProcessInstance instance =   runtimeService.startProcessInstanceByKey("technologyCorrespondenceClass",varibales);
        //奖项申报类
  //      ProcessInstance instance =   runtimeService.startProcessInstanceByKey("technologyAwardApplicationCategory",varibales);
        //技术类其他文件
   //    ProcessInstance instance =  runtimeService.startProcessInstanceByKey("technologyOtherTechnology",varibales);
        //经营及日常文件用印邀标文件查询函
    //   ProcessInstance instance =  runtimeService.startProcessInstanceByKey("businessInvitationToTenderDocumentsInquiryLetter",varibales);
        //经营以及日常文件用印其他经营日常文件
      ProcessInstance instance =  runtimeService.startProcessInstanceByKey("businessAndRoutineDocumentsShallBePrintedOnOtherRoutineBusinessDocuments",varibales);
//        4、输出内容
        System.out.println("流程定义ID：" + instance.getProcessDefinitionId());
        System.out.println("流程实例ID：" + instance.getId());
        System.out.println("当前活动的ID：" + instance.getActivityId());
    }


    //流程循环执行到一个角色，如果是并行节点，需要拿到后面的一个节点
    @Test
    public void testComplete() {
        String processId = "cfaa586c-2f50-11ed-9a35-00ff251e0e80";
//        获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        TaskQuery taskQuery = taskService.createTaskQuery().processInstanceId(processId);
        List<Task> list = taskQuery.list();
        for (int i = 0; i < list.size(); i++) {
            Task task = list.get(i);
            try{
                taskService.complete(task.getId());
                System.out.println(task.getTaskDefinitionKey());
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

        }

    }

}
